
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// using AppEventUtils;
using System;
using System.Linq;
using AppEventUtils;


namespace CardEffects
{
    // [CreateAssetMenu(fileName = "CardEffect_ManualRefreshListener", menuName = "CardSystem/CardEffect_ManualRefreshListener")]
    // public class CardEffect_ManualRefreshListener : BaseCardEffectModule
    
    public class CardEffect_ManualRefreshListener : BaseCardEffectModule
    {
        public int delta_atk = 1;
        public int delta_hp = 0;
        public int buff_id = 3;
        
        public override void Apply(GameObject card, CardUseInfo useInfo = null)
        {
            var card_comp = card.GetComponent<CardController>();
            var buff_comp = card.GetComponent<BuffHandle>();
            
            // 判断是否buff_data为1的buff已经存在
            bool exists = buff_comp.buffList.Any(buff_info => buff_info.buffData.id == buff_id);

            if (exists)
                return;
            
            var buff_data = ScriptableObject.CreateInstance<BuffData>();

            buff_data.id = buff_id;
            buff_data.className = GetType().ToString();
            buff_data.buffName = "刷新变强Buff";
            buff_data.description = $"每手动刷新一次, 就获得+{delta_atk}/{delta_hp}";
            buff_data.isForever = true;
            // buff_data.maxStack = BaseInfoManager.instance.MAX_BUFF_STACK;
            buff_data.maxStack = 1;
            
            // var add_buff_module = ScriptableObject.CreateInstance<BuffEffect_ChangeCardProperty>();
            var add_buff_module = new BuffEffect_ChangeCardProperty();
            add_buff_module.delta_atk = delta_atk;
            add_buff_module.delta_hp = delta_hp;
            // buff_data.OnCreate = add_buff_module;
            buff_data.OnStore = add_buff_module;
            buff_data.OnEventCallBack = add_buff_module;
            
            var remove_buff_module = new BuffEffect_ChangeCardProperty();
            remove_buff_module.delta_atk = -delta_atk;
            remove_buff_module.delta_hp = -delta_hp;
            buff_data.OnRemove = remove_buff_module;
            
            var buffInfo = new BuffInfo();
            buffInfo.buffData = buff_data;
            buffInfo.curStack = 1;
            buffInfo.target = card;
            buffInfo.creator = card;
            
            var buff_handle = card_comp.GetComponent<BuffHandle>();

            buff_handle.AddBuff(buffInfo);
            BdPackage.BdUtils.log($"--- On init CardEffect_ManualRefreshListener!");
            
            var event_i = EventManager.instance.add_event(eventType: GameEventTypeEnum.OnManualRefreshStore.ToString(), removeWhenRunOnce: false,
                event_callback: (sender, args) => {
                    // add_buff_module.Apply(buffInfo);
                    buff_data.OnEventCallBack.Apply(buffInfo);
                    return "";
                });
            
            buff_comp.add_event_listener(event_i);

            // var eventName = $"shield__{card_comp.card_data.card_name}__{GetType().Name}";
            // BdPackage.BdUtils.log($"========= eventName 0: {eventName}, bool: {BdPackage.BdUtils.conv_to_bool(eventName)}");
            //
            // var event_i = EventManager.instance.add_event(
            //     eventType: GameEventTypeEnum.OnRefreshStore.ToString(),
            //     removeWhenRunOnce: false,
            //     eventName: eventName,
            //     event_callback: (sender, args) => { add_buff_module.Apply(buffInfo);
            //         return "";
            //     });
            // event_i.EventName = eventName;
            // buff_comp.add_event_listener(event_i);
        }
    }
}

